//本文件填写Rogue-获得随机装备（假事件）事件执行周期内相关阶段函数
//【记得经常使用 Ctrl+S 保存修改，只有保存以后才能在编辑事件时生效】
//可引用变量 ArgBox - 该事件从上层事件继承的参数盒子

/// <summary>
/// 在此区域填写自定义的using指令
/// 注意：一般事件制作不需要填写; 在你明确知道每个using指令的意义和有明确使用需求的情况下再填写此指令集
/// </summary>
#region CustomUsings
public class Bar
{
	public static List<List<short>> _accessoryTemplateIdsByLevel;
	public static List<List<short>> _armourTemplateIdsByLevel;
	public static List<List<short>> _weaponTemplateIdsByLevel;
	public static List<List<short>> _foodTemplateIdsByLevel;
	public static List<List<short>> _teawineTemplateIdsByLevel;
}
public class RandomLevelGeneratorForItem
{
	public static List<List<int>> LevelProbForProgress = new List<List<int>> {
		new List<int> {40, 30, 30, 0, 0 , 0, 0, 0, 0},
		new List<int> {30, 30, 30, 10,0 , 0, 0, 0, 0},
		new List<int> {20, 20, 30, 20,10, 0, 0, 0, 0},
		new List<int> {10, 20, 25, 20,10,10, 0, 0, 0},
		new List<int> {10, 20, 20, 10,10,10,10, 0, 0},
		new List<int> {10, 15, 15, 15,10,10,10,10, 0},
		new List<int> {0 , 10, 15, 15,10,10,10,10,10},
		new List<int> {0 , 0 , 10, 15,15,15,10,10,10},
		new List<int> {0 , 0,  0,  15,15,15,15,15,10},
	};

	public static int getRandomLevel(Random rng, int stage) {
		int total = 0;
		LevelProbForProgress[stage].ForEach((int v) => {
			total += v;
		});
		float acc = 0;
		List<float> accumulate_probs = new List<float>();
		LevelProbForProgress[stage].ForEach((int v) => {
			acc += (float)v / total;
			accumulate_probs.Add(acc);
		});
		
		var v = rng.NextDouble();
		int level = 0;
		for (int i = 0; i < 9; i++)
		{
			if (v <= accumulate_probs[i])
			{
				return i;
			}
		}
		// accumulate_probs.ForEach((float thr) => {
		// 	if (v <= thr)
		// 	{
		// 		return level;
		// 	}
		// 	level += 1;
		// });
		return -1;
	}
}
#endregion

/// <summary>
/// 该事件监听的触发点被触发后，该接口检测事件是否满足执行条件，如果不满足则不执行该事件
/// </summary>
/// <returns>true - 可以执行 false - 忽略触发不执行</returns>
public override bool OnCheckEventCondition()
{
	//TODO
	return true;
}

/// <summary>
/// 该事件被触发且满足执行条件，事件显示前调用
/// 一般用于向参数盒子中准备事件链需要用到的参数
/// </summary>
public override void OnEventEnter()
{
	Random rng = new Random();
	EventHelper.Log("OnEventEnter assadsdas");
	// 初始化物品列表（根据品级分层）
	// Foo;
	if (Bar._accessoryTemplateIdsByLevel == null) {
		// EventHelper.Log("Initailizae");
		Bar._accessoryTemplateIdsByLevel = new List<List<short>>();
		EventHelper.Log("start init _accessoryTemplateIdsByLevel");
		for (int grade = 0; grade < 9; grade++)
		{
			Bar._accessoryTemplateIdsByLevel.Add(new List<short>());
		}
		for (int i = 0; i < Config.Accessory.Instance.Count; i++)
		{
			var config = Config.Accessory.Instance[i];
			var grade = config.Grade;
			EventHelper.Log($"{config.TemplateId} {config.Name} grade = {grade}");
			Bar._accessoryTemplateIdsByLevel[grade].Add(config.TemplateId);
		}
	}
	if (Bar._armourTemplateIdsByLevel == null) {
		// EventHelper.Log("Initailizae");
		Bar._armourTemplateIdsByLevel = new List<List<short>>();
		EventHelper.Log("start init _armourTemplateIdsByLevel");
		for (int grade = 0; grade < 9; grade++)
		{
			Bar._armourTemplateIdsByLevel.Add(new List<short>());
		}
		for (int i = 0; i < Config.Armor.Instance.Count; i++)
		{
			var config = Config.Armor.Instance[i];
			var grade = config.Grade;

			if (config.TemplateId >= 522 && config.TemplateId <= 541)
			{
				EventHelper.Log($"skip {config.TemplateId} {config.Name} grade = {grade}");
				continue;
			}

			EventHelper.Log($"{config.TemplateId} {config.Name} grade = {grade}");
			Bar._armourTemplateIdsByLevel[grade].Add(config.TemplateId);
		}
	}
	if (Bar._weaponTemplateIdsByLevel == null) {
		// EventHelper.Log("Initailizae");
		Bar._weaponTemplateIdsByLevel = new List<List<short>>();
		EventHelper.Log("start init _weaponTemplateIdsByLevel");
		for (int grade = 0; grade < 9; grade++)
		{
			Bar._weaponTemplateIdsByLevel.Add(new List<short>());
		}
		for (int i = 0; i < Config.Weapon.Instance.Count; i++)
		{
			var config = Config.Weapon.Instance[i];
			var grade = config.Grade;

			if (config.TemplateId < 3 || (config.TemplateId >= 831 && config.TemplateId <= 883)) 
			{
				EventHelper.Log($"skip {config.TemplateId} {config.Name} grade = {grade}");
				continue;
			}
			
			EventHelper.Log($"{config.TemplateId} {config.Name} grade = {grade}");
			Bar._weaponTemplateIdsByLevel[grade].Add(config.TemplateId);
		}
	}
	if (Bar._foodTemplateIdsByLevel == null) {
		// EventHelper.Log("Initailizae");
		Bar._foodTemplateIdsByLevel = new List<List<short>>();
		EventHelper.Log("start init _foodTemplateIdsByLevel");
		for (int grade = 0; grade < 9; grade++)
		{
			Bar._foodTemplateIdsByLevel.Add(new List<short>());
		}
		for (int i = 0; i < Config.Food.Instance.Count; i++)
		{
			var config = Config.Food.Instance[i];
			var grade = config.Grade;
			EventHelper.Log($"{config.TemplateId} {config.Name} grade = {grade}");
			Bar._foodTemplateIdsByLevel[grade].Add(config.TemplateId);
		}
	}
	if (Bar._teawineTemplateIdsByLevel == null) {
		// EventHelper.Log("Initailizae");
		Bar._teawineTemplateIdsByLevel = new List<List<short>>();
		EventHelper.Log("start init _teawineTemplateIdsByLevel");
		for (int grade = 0; grade < 9; grade++)
		{
			Bar._teawineTemplateIdsByLevel.Add(new List<short>());
		}
		for (int i = 0; i < Config.TeaWine.Instance.Count; i++)
		{
			var config = Config.TeaWine.Instance[i];
			var grade = config.Grade;
			EventHelper.Log($"{config.TemplateId} {config.Name} grade = {grade}");
			Bar._teawineTemplateIdsByLevel[grade].Add(config.TemplateId);
		}
	}
	// Foo._combatSkillTemplateIdsByLevel = new List<List<short>>();
	// var _accessoryTemplateIdsByLevel = Bar._accessoryTemplateIdsByLevel;
	// _combatSkillTemplateIdsByLevel = new List<List<short>>();

	var taiwu = ArgBox.GetCharacter(EventArgBox.RoleTaiwu);
	// for (int i = 1; i <= 3; i++)
	// {
	// 	bool[] pageIsDirect = new bool[]
	// 	{
	// 		true, // rng.NextDouble() > 0.5,
	// 		true, // rng.NextDouble() > 0.5,
	// 		true, // rng.NextDouble() > 0.5,
	// 		true, // rng.NextDouble() > 0.5,
	// 		true, // rng.NextDouble() > 0.5
	// 	};
	// 	int skillLevel = RandomLevelGenerator.getRandomLevel(rng, (GameData.Domains.DomainManager.World.GetMainStoryLineProgress() - 100) / 5);
	// 	int skillIndex = rng.Next(_combatSkillTemplateIdsByLevel[skillLevel].Count);
	// 	ItemKey combatSkillBook = EventHelper.GetCombatSkillBook(
	// 		taiwu.GetId(),
	// 		_combatSkillTemplateIdsByLevel[skillLevel][skillIndex],
	// 		(sbyte)rng.Next(5),
	// 		pageIsDirect,
	// 		5);
	// 	EventHelper.RemoveInventoryItem(taiwu, combatSkillBook, 1, false); // 暂时从背包里删除该书
	// 	ArgBox.Set($"CSB{i}", combatSkillBook);
	// 	ArgBox.Set($"CS{i}", _combatSkillTemplateIdsByLevel[skillLevel][skillIndex]);
	// 	EventHelper.Log($"CS{i}: skill= {_combatSkillTemplateIdsByLevel[skillLevel][skillIndex]}");
	// }

	var context = GameData.Domains.DomainManager.TaiwuEvent.MainThreadDataContext;
	int itemLevel;
	int itemIndex;
	var stage = (GameData.Domains.DomainManager.World.GetMainStoryLineProgress() - 100) / 5;

	itemLevel = RandomLevelGeneratorForItem.getRandomLevel(rng, stage);
	itemIndex = rng.Next(Bar._accessoryTemplateIdsByLevel[itemLevel].Count);
	ItemKey item1 = GameData.Domains.DomainManager.Item.CreateAccessory(context, (short)Bar._accessoryTemplateIdsByLevel[itemLevel][itemIndex]);

	itemLevel = RandomLevelGeneratorForItem.getRandomLevel(rng, stage);
	itemIndex = rng.Next(Bar._armourTemplateIdsByLevel[itemLevel].Count);
	ItemKey item2 = GameData.Domains.DomainManager.Item.CreateArmor(context, (short)Bar._armourTemplateIdsByLevel[itemLevel][itemIndex]);
	itemIndex = rng.Next(Bar._armourTemplateIdsByLevel[itemLevel].Count);
	ItemKey item21 = GameData.Domains.DomainManager.Item.CreateArmor(context, (short)Bar._armourTemplateIdsByLevel[itemLevel][itemIndex]);
	itemIndex = rng.Next(Bar._armourTemplateIdsByLevel[itemLevel].Count);
	ItemKey item22 = GameData.Domains.DomainManager.Item.CreateArmor(context, (short)Bar._armourTemplateIdsByLevel[itemLevel][itemIndex]);

	itemLevel = RandomLevelGeneratorForItem.getRandomLevel(rng, stage);
	itemIndex = rng.Next(Bar._weaponTemplateIdsByLevel[itemLevel].Count);
	ItemKey item3 = GameData.Domains.DomainManager.Item.CreateWeapon(context, (short)Bar._weaponTemplateIdsByLevel[itemLevel][itemIndex]);
	itemIndex = rng.Next(Bar._weaponTemplateIdsByLevel[itemLevel].Count);
	ItemKey item31 = GameData.Domains.DomainManager.Item.CreateWeapon(context, (short)Bar._weaponTemplateIdsByLevel[itemLevel][itemIndex]);
	itemIndex = rng.Next(Bar._weaponTemplateIdsByLevel[itemLevel].Count);
	ItemKey item32 = GameData.Domains.DomainManager.Item.CreateWeapon(context, (short)Bar._weaponTemplateIdsByLevel[itemLevel][itemIndex]);

	itemLevel = RandomLevelGeneratorForItem.getRandomLevel(rng, stage);
	itemIndex = rng.Next(Bar._foodTemplateIdsByLevel[itemLevel].Count);
	ItemKey item4 = GameData.Domains.DomainManager.Item.CreateFood(context, (short)Bar._foodTemplateIdsByLevel[itemLevel][itemIndex]);

	itemLevel = RandomLevelGeneratorForItem.getRandomLevel(rng, stage);
	itemIndex = rng.Next(Bar._teawineTemplateIdsByLevel[itemLevel].Count);
	ItemKey item5 = GameData.Domains.DomainManager.Item.CreateTeaWine(context, (short)Bar._teawineTemplateIdsByLevel[itemLevel][itemIndex]);

	ArgBox.Set($"ITEM1", item1);
	ArgBox.Set($"ITEM2", item2);
	ArgBox.Set($"ITEM3", item21);
	ArgBox.Set($"ITEM4", item22);  // more weapon
	ArgBox.Set($"ITEM5", item3);
	ArgBox.Set($"ITEM6", item31);
	ArgBox.Set($"ITEM7", item32);
	ArgBox.Set($"ITEM8", item4);
	ArgBox.Set($"ITEM9", item5);


	EventHelper.ShowGetItemPageForItems(new List<ValueTuple<ItemKey, int>>
	{
		new ValueTuple<ItemKey, int>(item1, 1),
		new ValueTuple<ItemKey, int>(item2, 1),
		new ValueTuple<ItemKey, int>(item21, 1),
		new ValueTuple<ItemKey, int>(item22, 1),
		new ValueTuple<ItemKey, int>(item3, 1),
		new ValueTuple<ItemKey, int>(item31, 1),
		new ValueTuple<ItemKey, int>(item32, 1),
		new ValueTuple<ItemKey, int>(item4, 1),
		new ValueTuple<ItemKey, int>(item5, 1),
	}, "4fc8c656-f608-4ad3-8359-07326ac2fa6b", this.ArgBox, false);
}

/// <summary>
/// 该事件执行完毕，即将退出该事件时调用
/// 一般用于从参数盒子中移除事件链中不需要用到的参数，或记录事件触发月份，确保后续触发几率计算
/// </summary>
public override void OnEventExit()
{
	//TODO
}

/// <summary>
/// 该事件即将显示前调用,获取被替换占位符后的事件显示内容
/// 如果事件文本预设满足系统默认的占位符替换方案，则可以不填写此接口
/// </summary>
public override string GetReplacedContentString()
{
	//TODO
	return string.Empty;
}

/// <summary>
/// 获取额外格式化使用的多语言字段
/// </summary>
/// <returns></returns>
public override List<string> GetExtraFormatLanguageKeys()
{
    return default;
}